package com.ng.framework.view.desktop.table;

import java.awt.Color;
import java.awt.Component;
import java.text.NumberFormat;
import java.util.Date;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;

import com.ng.framework.util.NGUtil;

public class NGTableCellRenderer extends JLabel implements TableCellRenderer {

	private static final long serialVersionUID = -2441779263108823840L;

	private Border noFocusBorder;
	
	public NGTableCellRenderer() {
		this.noFocusBorder = new EmptyBorder(1, 2, 1, 2);
		this.setOpaque(true);
		this.setBorder(noFocusBorder);
	}

	@Override
	public Component getTableCellRendererComponent(final JTable table, Object value, boolean isSelected, boolean hasFocus, final int row,
			int column) {

		Color foreground = null;
		Color background = null;
		NumberFormat numberFormat = null;
		Integer alignment = null;
		boolean fraction = false;

		TableModel model = table.getModel();
		if (model instanceof NGTableModel) {
			NGCellAttributes cellAtt = ((NGTableModel) model).getCellAttributes();

			foreground = cellAtt.getCellForeground(row, column);
			background = cellAtt.getCellBackground(row, column);
			fraction = cellAtt.isFractionColumn(column);

			if (value != null) {
				numberFormat = cellAtt.getNumberFormat(value.getClass());
			}
			alignment = cellAtt.getCellAlignment(row, column);
		}
		
		if (isSelected) {
			this.setForeground((foreground != null) ? foreground : table.getSelectionForeground());
			this.setBackground(table.getSelectionBackground());
		} else {
			if (row % 2 == 0) {
				this.setBackground((background != null) ? background : new Color(UIManager.getColor("Table.alternateRowColor").getRGB()));
			} else {
				this.setBackground((background != null) ? background : new Color(table.getBackground().getRGB()));
			}
			this.setForeground((foreground != null) ? foreground : table.getForeground());
		}

		this.setFont(table.getFont());

		if (hasFocus) {
			setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
		} else {
			setBorder(noFocusBorder);
		}

		if (fraction) {
			this.setText((value == null) ? "" : NGUtil.doubleToFraction((Double) value, true));
		} else if (numberFormat != null) {
			this.setText(numberFormat.format(value));
		} else {
			this.setText((value == null) ? "" : value.toString());
		}

		if (alignment != null) {
			this.setHorizontalAlignment(alignment);
		} else {
			if (value instanceof Number || value instanceof Date) {
				this.setHorizontalAlignment(SwingConstants.RIGHT);
			} else {
				this.setHorizontalAlignment(SwingConstants.LEFT);
			}
		}
		
		this.setIcon(null);
		if (value instanceof Icon) {
			this.setText(null);
			this.setIcon((Icon)value);
			this.setHorizontalAlignment(SwingConstants.CENTER);
		}
		this.setToolTipText(this.getText());
		
		return this;
	}

}
